const express = require('express')
const router = express.Router()
const db = require('../../connection/Connection')
const jwt = require('jsonwebtoken')
const SECRET = 'asdfta219*&%^jWJSHFJ'

//获取所有管理者权限
router.post('/getAccessInfo', async (req, res) => {
    const result = jwt.verify(req.body.token, SECRET)
    let sql = 'select departmentId from user_role where username = ?'
    db.base(sql, result.username, response_temp => {
        if (response_temp != null) {
            if (response_temp[0].departmentId == 999) {
                sql = 'select * from user_role'
                db.base(sql, null, response => {
                    console.log(response)
                    res.json({
                        result: "success",
                        reason: response
                    })
                })
            } else {
                res.json({
                    result: "error",
                    reason: "无权限"
                })
            }
        } else {
            res.json({
                result: "error",
                reason: "无权限"
            })
        }
    })

})

//获取所有权限角色
router.post('/getRoleInfo', async (req, res) => {
    const result = jwt.verify(req.body.token, SECRET)
    let sql = 'select departmentId from user_role where username = ?'
    db.base(sql, result.username, response_temp => {
        if (response_temp != null) {
            if (response_temp[0].departmentId == 999) {
                sql = 'select * from role'
                db.base(sql, null, response => {
                    console.log(response)
                    let data = []
                    for (i = 0; i < response.length; i++) {
                        data[i] = {
                            "username": response[i].username,
                            "departmentId": response[i].department_id,
                            "roleName": response[i].role_name,
                            "hr": response[i].hr
                        }
                    }
                    res.json({
                        data
                    })
                })
            } else {
                res.json({
                    result: "error",
                    reason: "无权限"
                })
            }
        } else {
            res.json({
                result: "error",
                reason: "无权限"
            })
        }
    })

})

module.exports = router
